import Vue from 'vue'
var token = {
    install(Vue) {
        Vue.prototype.$token = {
            setCookie: (name, value) => {
                if (!process.browser) return
                let exp = new Date();
                exp.setTime(exp.getTime() + 60 * 60 * 1000 * 12);
                let v = typeof value == 'object' ? JSON.stringify(value) : value
                document.cookie =
                    name + "=" + escape(v) + ";expires=" + exp.toGMTString() + ";path=/";
            },
            getCookie: name => {
                if (!process.browser) return
                let arr;
                let reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
                if ((arr = document.cookie.match(reg))) {
                    let v
                    try {
                        v = JSON.parse(unescape(arr[2]))
                    } catch (error) {
                        v = unescape(arr[2])
                    }
                    return v;
                } else {
                    return null;
                }
            },
            delCookie: name => {
                if (!process.browser) return
                let exp = new Date();
                exp.setTime(exp.getTime() - 60 * 60 * 1000 * 12);
                let cval = Vue.prototype.$token.getCookie(name);
                if (cval != null) {
                    document.cookie =
                        name + "=" + cval + ";expires=" + exp.toGMTString() + ";path=/";
                }
            }
        };

    }
}
Vue.use(token)